<?php
    defined('M2_MICRO') or die('Direct Access to this location is not allowed.');

    /**
      * Default model class
      * @name $model
      * @package M2 Micro Framework
      * @subpackage Modules
      * @author Alexander Chaika
      */
    class Model extends Message {
        protected $cid;
        protected $database;

        /**
          * Model class constructor with DB init
          */
        public function __construct() {
            // get database object
            $this->database = Database::getInstance();

            // setup DB connection
            if (empty($this->cid)) {
                $this->cid = $this->database->connect();
            }
        }
        
        /**
          * Get coords from yamaps service
          * @param string $address
          * @return array $coords
          */
        public function getCoords($address) {
            // open CURL session
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_FAILONERROR, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);

            // get coords
            $url = 'http://geocode-maps.yandex.ru/1.x/?geocode='.urlencode($address).'&key=APYbjEsBAAAAFD33IQIAua2B4OGy4WyH6KClVdNY3BiW1d8AAAAAAAAAAAA44Al198Kfb-CDGWSNujQ_AG6X8A==&format=json&results=1&plng=ru';
            curl_setopt($curl, CURLOPT_URL, $url); // set url to post to
            $result = json_decode(curl_exec($curl)); // run the whole process
            $coords = explode(' ', $result->response->GeoObjectCollection->featureMember[0]->GeoObject->Point->pos);
            
            // close CURL
            curl_close($curl);
            
            return $coords;
        }
    }
?>